package net.uri;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Authenticator;
import java.net.PasswordAuthentication;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class DialogAuthenticator extends Authenticator
{
	private JDialog			passwordDialog;
	private JLabel			mainLable		= new JLabel("please enter username and password");
	private JLabel			userLable		= new JLabel("username:");
	private JLabel			passwoedLabel	= new JLabel("Password");
	private JTextField		usernameField	= new JTextField(20);
	private JPasswordField	passwordField	= new JPasswordField(20);
	private JButton			okButton		= new JButton("ok");
	private JButton			cancelButton	= new JButton("cancel");
	
	public DialogAuthenticator()
	{
		this("", new JFrame());
	}
	
	public DialogAuthenticator(String username)
	{
		this(username, new JFrame());
	}
	
	public DialogAuthenticator(String username, JFrame parent)
	{
		this.passwordDialog = new JDialog(parent, true);
		Container pane = passwordDialog.getContentPane();
		pane.add(mainLable);
		JPanel p2 = new JPanel();
		p2.add(userLable);
		p2.add(usernameField);
		usernameField.setText(username);
		pane.add(p2);
		JPanel p3 = new JPanel();
		p3.add(passwoedLabel);
		p3.add(passwordField);
		pane.add(p3);
		JPanel p4 = new JPanel();
		p4.add(okButton);
		p4.add(cancelButton);
		passwordDialog.pack();
		ActionListener al = new OKResponse();
		okButton.addActionListener(al);
		usernameField.addActionListener(al);
		passwordField.addActionListener(al);
		cancelButton.addActionListener(al);
	}
	
	public void show()
	{
		String propt = this.getRequestingPrompt();
		if (propt == null)
		{
			String site = this.getRequestingSite().getHostName();
			String protocal = this.getRequestingProtocol();
			int port = this.getRequestingPort();
			if (site != null && protocal != null)
			{
				propt = protocal + "://" + site;
				if (port > 0)
				{
					propt += ":" + port;
				}
			}
			else
			{
				propt = "";
			}
			mainLable.setText("please enter username and password for" + propt + ":");
			passwordDialog.pack();
			passwordDialog.setVisible(true);
		}
	}
	
	PasswordAuthentication	response	= null;
	
	public class CancelResponse implements ActionListener
	{
		
		@Override
		public void actionPerformed(ActionEvent e)
		{
			passwordDialog.setVisible(false);
			passwordField.setText("");
			response = null;
		}
		
	}
	
	public class OKResponse implements ActionListener
	{
		
		@Override
		public void actionPerformed(ActionEvent e)
		{
			passwordDialog.setVisible(false);
			char[] password = passwordField.getPassword();
			String userName = usernameField.getText();
			passwordField.setText("");
			response = new PasswordAuthentication(userName, password);
		}
		
	}
	
	public PasswordAuthentication getPasswordAuthentication()
	{
		this.show();
		return this.response;
	}
	
}
